/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import entidad.Proveedor;
import java.awt.Color;

/**
 *
 * @author Andrés,Daniel,Osmond
 */
public class VisualizarProveedores extends javax.swing.JPanel {

    /**
     * Creates new form VisualizarProveedores
     */
    private AdministarProveedores administrarProveedores;

    public VisualizarProveedores(AdministarProveedores administrarProveedores) {
        initComponents();
        this.administrarProveedores = administrarProveedores;
        deshabilitarTodo();
        mostrarAviso("");
    }

    public void deshabilitarTodo() {
        nitTF.setEditable(false);
        nombreTF.setEditable(false);
        nombreContactoTF.setEditable(false);
        telefonoTF.setEditable(false);
        ubicacionTF.setEditable(false);
        cedulaTF.setEditable(false);

        cancelarB.setVisible(false);
        guardarB.setVisible(false);
    }

    public void habilitarTodo() {
        nitTF.setEditable(true);
        nombreTF.setEditable(true);
        nombreContactoTF.setEditable(true);
        telefonoTF.setEditable(true);
        ubicacionTF.setEditable(true);
        cedulaTF.setEditable(true);

        cancelarB.setVisible(true);
        guardarB.setVisible(true);
        mostrarAviso("");
    }

    public void habilitarModif() {
        nitTF.setEditable(false);
        nombreTF.setEditable(true);
        nombreContactoTF.setEditable(true);
        telefonoTF.setEditable(true);
        ubicacionTF.setEditable(true);
        cedulaTF.setEditable(true);

        cancelarB.setVisible(true);
        guardarB.setVisible(true);
        mostrarAviso("");
    }

    public void visualizar(Proveedor proveedor) {
        nitTF.setText("" + proveedor.getNit());
        nombreTF.setText(proveedor.getNombre());
        nombreContactoTF.setText(proveedor.getNombredeContacto());
        telefonoTF.setText("" + proveedor.getTelefono());
        ubicacionTF.setText(proveedor.getUbicacion());
        cedulaTF.setText(proveedor.getCedula().toString());
    }

    public void limpiar() {
        nitTF.setText("");
        nombreTF.setText("");
        nombreContactoTF.setText("");
        telefonoTF.setText("");
        ubicacionTF.setText("");
        cedulaTF.setText("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelVisualizarProveedor = new javax.swing.JPanel();
        nitL = new javax.swing.JLabel();
        nitTF = new javax.swing.JTextField();
        nombre = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        telefonoL = new javax.swing.JLabel();
        telefonoTF = new javax.swing.JTextField();
        ubicacionL = new javax.swing.JLabel();
        ubicacionTF = new javax.swing.JTextField();
        nombreContactoL = new javax.swing.JLabel();
        nombreContactoTF = new javax.swing.JTextField();
        cedulaL = new javax.swing.JLabel();
        cedulaTF = new javax.swing.JTextField();
        guardarB = new javax.swing.JButton();
        cancelarB = new javax.swing.JButton();
        mensajeL = new javax.swing.JLabel();

        nitL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        nitL.setText("NIT");

        nitTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        nombre.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        nombre.setText("Nombre");

        nombreTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        telefonoL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        telefonoL.setText("Teléfono");

        telefonoTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        ubicacionL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        ubicacionL.setText("Ubicación");

        ubicacionTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        nombreContactoL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        nombreContactoL.setText("Nombre de Contacto");

        nombreContactoTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        cedulaL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        cedulaL.setText("Cédula");

        cedulaTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        javax.swing.GroupLayout panelVisualizarProveedorLayout = new javax.swing.GroupLayout(panelVisualizarProveedor);
        panelVisualizarProveedor.setLayout(panelVisualizarProveedorLayout);
        panelVisualizarProveedorLayout.setHorizontalGroup(
            panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                        .addComponent(ubicacionL, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ubicacionTF))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVisualizarProveedorLayout.createSequentialGroup()
                        .addComponent(telefonoL, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(telefonoTF))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVisualizarProveedorLayout.createSequentialGroup()
                        .addComponent(cedulaL, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cedulaTF))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVisualizarProveedorLayout.createSequentialGroup()
                        .addComponent(nombreContactoL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nombreContactoTF))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVisualizarProveedorLayout.createSequentialGroup()
                        .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nombreTF))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVisualizarProveedorLayout.createSequentialGroup()
                        .addComponent(nitL, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nitTF, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelVisualizarProveedorLayout.setVerticalGroup(
            panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nitL)
                    .addComponent(nitTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(nombre))
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(nombreContactoL))
                    .addComponent(nombreContactoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(cedulaL))
                    .addComponent(cedulaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(telefonoL))
                    .addComponent(telefonoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(panelVisualizarProveedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVisualizarProveedorLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(ubicacionL))
                    .addComponent(ubicacionTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        guardarB.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        cancelarB.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cancelarB.setText("Cancelar");
        cancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBActionPerformed(evt);
            }
        });

        mensajeL.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
        mensajeL.setForeground(new java.awt.Color(255, 0, 0));
        mensajeL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        mensajeL.setText("Mensaje");
        mensajeL.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                mensajeLFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelVisualizarProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 2, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mensajeL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 197, Short.MAX_VALUE)
                        .addComponent(guardarB, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelarB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mensajeL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelVisualizarProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardarB)
                    .addComponent(cancelarB))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBActionPerformed
        limpiar();
        deshabilitarTodo();
        administrarProveedores.actualizarResultados();

    }//GEN-LAST:event_cancelarBActionPerformed

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed

        if(!VentanaPrincipal.esLong(nitTF.getText())){
            mostrarAviso("El campo NIT solo admite valores numéricos", Color.RED);
            return;
        }
        if(!VentanaPrincipal.esLong(telefonoTF.getText())){
            mostrarAviso("El campo telefono solo admite valores numéricos", Color.RED);
            return;
        }
        if(!VentanaPrincipal.esLong(cedulaTF.getText())){
            mostrarAviso("El campo cédula solo admite valores numéricos", Color.RED);
            return;
        }
        
        Proveedor nuevo = new Proveedor(nombreTF.getText(),
                                        Long.parseLong(nitTF.getText()),
                                        nombreContactoTF.getText(),
                                        Long.parseLong(telefonoTF.getText()),
                                        Long.parseLong(cedulaTF.getText()),
                                        ubicacionTF.getText());
        
        String mensaje = "";
        if (administrarProveedores.getSeleccionado() == null) {
            mensaje = VentanaPrincipal.controlProveedor.agregar(nuevo);
            if (!mensaje.equals("Proveedor agregado exitosamente!")){
                mostrarAviso(mensaje, Color.RED);
                return;
            }
        } else {
            mensaje = VentanaPrincipal.controlProveedor.modificar(nuevo);
            if (!mensaje.equals("Proveedor modificado exitosamente!")){
                mostrarAviso(mensaje, Color.RED);
                return;
            }
        }

        mostrarAviso(mensaje, Color.BLUE);
        limpiar();
        deshabilitarTodo();
        administrarProveedores.actualizarResultados();
        
    }//GEN-LAST:event_guardarBActionPerformed

    private void mensajeLFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_mensajeLFocusLost
        mostrarAviso("");
    }//GEN-LAST:event_mensajeLFocusLost

    public static void mostrarAviso(String mensaje) {
        mensajeL.setText(mensaje);
        mensajeL.setForeground(Color.red);
    }

    public static void mostrarAviso(String mensaje, java.awt.Color s) {
        mensajeL.setText(mensaje);
        mensajeL.setForeground(s);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarB;
    private javax.swing.JLabel cedulaL;
    private javax.swing.JTextField cedulaTF;
    private javax.swing.JButton guardarB;
    private static javax.swing.JLabel mensajeL;
    private javax.swing.JLabel nitL;
    private javax.swing.JTextField nitTF;
    private javax.swing.JLabel nombre;
    private javax.swing.JLabel nombreContactoL;
    private javax.swing.JTextField nombreContactoTF;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JPanel panelVisualizarProveedor;
    private javax.swing.JLabel telefonoL;
    private javax.swing.JTextField telefonoTF;
    private javax.swing.JLabel ubicacionL;
    private javax.swing.JTextField ubicacionTF;
    // End of variables declaration//GEN-END:variables
}
